<?php

if (eregi(".inc.php",$HTTP_SERVER_VARS['PHP_SELF']) || eregi(".inc.php",$_SERVER['PHP_SELF'])) {
	echo "<html>\r\n<head>\r\n<title>Forbidden 403</title>\r\n</head>\r\n<body><h3>Forbidden 403</h3>\r\nThe document you are requesting is forbidden.\r\n</body>\r\n</html>";
	exit;
}


		
// get listing options		
$getListOptions = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'prodListings'");
$listOptions = mysql_fetch_assoc($getListOptions);
$prodOptions = explode(":", $listOptions['value']);


// get details of single product
	
// use merchant product ids
	if ($navOpt[12] == 1) {
	$theprodID = 'merchantProdID';
	} else {
	$theprodID = 'dbProdID';
	}
$sql = sprintf("SELECT * FROM affiliSt_products1 WHERE prodDB = %d AND $theprodID = %s",
			quote_smart($_GET['proddb']),
			quote_smart($_GET['pid']));
$getPDetails = mysql_query($sql, $databaseConnect) or die(mysql_error());
$pDetails = mysql_fetch_assoc($getPDetails);

// price compare products listing
$getComparison = mysql_query("SELECT * FROM affiliSt_products1 WHERE (prodName LIKE '%".str_replace($allCharacters, "", $pDetails['prodName'])."%') AND dbProdID != '".$pDetails['dbProdID']."' ORDER BY prodPrice ASC LIMIT 0, ".$pPerPage[4]."");
$comparison = mysql_fetch_assoc($getComparison);

// get lowest price
$getLowPrice = mysql_query("SELECT * FROM affiliSt_products1 WHERE (prodName LIKE '%".str_replace($allCharacters, "", $pDetails['prodName'])."%') AND dbProdID != '".$pDetails['dbProdID']."' ORDER BY prodPrice ASC LIMIT 0,1");
$lowprice = mysql_fetch_assoc($getLowPrice);
if ($lowprice['prodPrice'] == 0.00) {
$thelowprice = 'N/A';
} else {
$thelowprice = $lowprice['prodCurrency'].$lowprice['prodPrice'];
}

// get highest price
$getHighPrice = mysql_query("SELECT * FROM affiliSt_products1 WHERE (prodName LIKE '%".str_replace($allCharacters, "", $pDetails['prodName'])."%') AND dbProdID != '".$pDetails['dbProdID']."' ORDER BY prodPrice DESC LIMIT 0,1");
$highprice = mysql_fetch_assoc($getHighPrice);
if ($highprice['prodPrice'] == 0.00 || $highprice['prodPrice'] == $lowprice['prodPrice']) {
$thehighprice = '';
} else {
$thehighprice = ' to '.$highprice['prodCurrency'].$highprice['prodPrice'];
}

$box_content = new XTemplate ("skins/".$configSkin['value']."/styleTemplates/compare.tpl");

if ($comparison['dbProdID'] != NULL && $navOpt[10] == 1) {

// pinch column values from head
$totalColumns = $pPerPage[2];
$columnCount = 0;
$productCount = 0;


        do {
		
			
		// get titles
		$getTitles = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'feedMemory".$comparison['prodDB']."'");
		$allTitles = mysql_fetch_assoc($getTitles);
		$listTitles = explode("-:-", $allTitles['value']);
		
		// thumb image
		if ($comparison['prodImageSmall'] == NULL) {
		$thumbImage = $comparison['prodImageURL'];
		} else {
		$thumbImage = $comparison['prodImageSmall'];
		}

					// use merchant product ids
					if ($navOpt[12] == 1) {
					$theprodID = $comparison['merchantProdID'];
					} else {
					$theprodID = $comparison['dbProdID'];
					}
					if ($navOpt[7] == 1) {
					// html perma
					$prodLink = $installDir['value'].'product/'.$comparison['prodDB'].'/'.$theprodID.'/'.urltxt($comparison['prodName']).'.html';
					$box_content->assign("IMAGELINK",$installDir['value'].'img/'.$comparison['prodDB'].'/'.$comparison['dbProdID'].'.jpg');
					} else {
					// php link
					$prodLink = $installDir['value'].'index.php?case=product&proddb='.$comparison['prodDB'].'&pid='.$theprodID;
					$box_content->assign("IMAGELINK",$thumbImage);
					}
					$box_content->assign("PRODLINK",$prodLink);
					
					$box_content->assign("GOLINK",$installDir['value'].'go.php?proddb='.$comparison['prodDB'].'&amp;l='.$comparison['dbProdID']);
					
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[21];
						} else {
						$theTitle = '';
						}
					$box_content->assign("NAME",$theTitle.$comparison['prodName']);
					
					if ($prodOptions[0] == 1 && $comparison['merchant'] != '') {
					$box_content->assign("MERCHANT",'<span class="styleProdMerchant">'.$comparison['merchant'].'</span><br />');
					}
					if ($prodOptions[1] == 1 && $comparison['merchantProdID'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[19];
						} else {
						$theTitle = '';
						}
					$box_content->assign("PRODID", '<span class="styleProdID">'.$theTitle.$comparison['merchantProdID'].'</span><br />');
					} else {
					$box_content->assign("PRODID",'');
					}
					if ($prodOptions[2] == 1 && $comparison['prodCategory'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[20];
						} else {
						$theTitle = '';
						}
					$box_content->assign("PRODCAT",'<span class="styleProdCategory">'.$theTitle.$comparison['prodCategory'].'</span><br />');
					} else {
					$box_content->assign("PRODCAT",'');
					}
					if ($prodOptions[3] == 1 && $comparison['prodBrand'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[22];
						} else {
						$theTitle = '';
						}
					$box_content->assign("BRAND",'<span class="styleProdBrand">'.$theTitle.$comparison['prodBrand'].'</span><br />');
					} else {
					$box_content->assign("BRAND",'');
					}
					if ($prodOptions[4] == 1 && $comparison['prodPromoText'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[24];
						} else {
						$theTitle = '';
						}
					$box_content->assign("PROMO",'<span class="styleProdPromo">'.$theTitle.$comparison['prodPromoText'].'</span><br />');
					} else {
					$box_content->assign("PROMO",'');
					}
					if ($prodOptions[5] == 1 && $comparison['prodDescription'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[23];
						} else {
						$theTitle = '';
						}
					$box_content->assign("DESCRIPTION",'<span class="styleProdDescription">'.$theTitle.$comparison['prodDescription'].'</span><br />');
					} else {
					$box_content->assign("DESCRIPTION",'');
					}
					if ($prodOptions[6] == 1 && $comparison['prodPrice'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[27];
						} else {
						$theTitle = '';
						}
						if ($comparison['prodPrice'] == 0.00) {
						$box_content->assign("PRICE",$theTitle.'<span class="styleProdPrice">N/A</span><br />');
						} else {
						$box_content->assign("PRICE",$theTitle.'<span class="styleProdPrice">'.$comparison['prodCurrency'].$comparison['prodPrice'].'</span><br />');
						}
					} else {
					$box_content->assign("PRICE",'');
					}
					if ($prodOptions[7] == 1 && $comparison['extraFieldA'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[28];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDA",'<span class="singleProdDescription">'.$theTitle.$comparison['extraFieldA'].'</span><br />');
					} else {
					$box_content->assign("EXTRAFIELDA",'');
					}
					if ($prodOptions[8] == 1 && $comparison['extraFieldB'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[29];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDB",'<span class="singleProdDescription">'.$theTitle.$comparison['extraFieldB'].'</span><br />');
					} else {
					$box_content->assign("EXTRAFIELDB",'');
					}
					if ($prodOptions[9] == 1 && $comparison['extraFieldC'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[30];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDC",'<span class="singleProdDescription">'.$theTitle.$comparison['extraFieldC'].'</span><br />');
					} else {
					$box_content->assign("EXTRAFIELDC",'');
					}
					if ($prodOptions[10] == 1 && $comparison['extraFieldD'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[31];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDD",'<span class="singleProdDescription">'.$theTitle.$comparison['extraFieldD'].'</span><br />');
					} else {
					$box_content->assign("EXTRAFIELDD",'');
					}
					if ($prodOptions[11] == 1 && $comparison['extraFieldE'] != '') {
						if ($prodOptions[12] == 1) {
						$theTitle = $listTitles[32];
						} else {
						$theTitle = '';
						}
					$box_content->assign("EXTRAFIELDE",'<span class="singleProdDescription">'.$theTitle.$comparison['extraFieldE'].'</span><br />');
					} else {
					$box_content->assign("EXTRAFIELDE",'');
					}
					if ($prodOptions[13] == 1 && $listTitles[33] != '') {
					$box_content->assign("MERCHANTLOGO",'<a href="'.$installDir['value'].'go.php?proddb='.$comparison['prodDB'].'&amp;l='.$comparison['dbProdID'].'" rel="nofollow" target="_blank"><img src="'.$installDir['value'].$listTitles[33].'" alt="'.$comparison['merchant'].'" border="0" /></a><br />');
					} else {
					$box_content->assign("MERCHANTLOGO",'');
					}
					
					
					$box_content->assign("INSTALLDIR",$installDir['value']);
					$box_content->assign("SKIN",$configSkin['value']);
					$box_content->assign("ENDROW","");
					
					if ($navOpt[13] == 1) {
					// get comments
					$getComments = mysql_query("SELECT * FROM affiliSt_comments WHERE attachedID = '".$comparison['prodName']."' AND approve = '1' AND section = 'products' ORDER BY date DESC");
					$comments = mysql_fetch_assoc($getComments);
					// total comments
					$sql = "SELECT count(*) as numRows FROM affiliSt_comments WHERE  attachedID = '".$comparison['prodName']."' AND approve = '1' AND section = 'products'";
					$query = mysql_query($sql);
					$aRow = mysql_fetch_array($query);
					//echo $aRow[numRows]
					
					$totalProdRating = 0;
						do {
						$totalProdRating = $totalProdRating + $comments['rating'];
						} while ($comments = mysql_fetch_assoc($getComments));
					
						if ($aRow[numRows] != 0) {
						$prodRating = $totalProdRating / $aRow[numRows];
						$prodRating = round($prodRating);
						}
					
						if ($aRow[numRows] == 0) {
						$box_content->assign("RATING",'<a href="'.$prodLink.'">Review this product</a><br />');
						} else {
						$box_content->assign("RATING",'<img src="'.$installDir['value'].'skins/'.$configSkin['value'].'/styleImages/'.$prodRating.'star.gif" width="84" height="15" alt="Rating: '.$prodRating.'" align="left" /> (<a href="'.$prodLink.'">'.$aRow[numRows].' reviews</a>)<br />');
						}
					
					
						if ((($prodOptions[13] == 1 && $listTitles[33] != '') || ($prodOptions[0] == 1 && $comparison['merchant'] != ''))) {
						// merchant comments
						$getMerchantComments = mysql_query("SELECT * FROM affiliSt_comments WHERE attachedID = '".urltxt($comparison['merchant'])."' AND approve = '1' AND section = 'stores' ORDER BY date DESC");
						$merchantComments = mysql_fetch_assoc($getMerchantComments);
						// total comments
						$sql = "SELECT count(*) as numRows FROM affiliSt_comments WHERE attachedID = '".urltxt($comparison['merchant'])."' AND approve = '1' AND section = 'stores'";
						$query = mysql_query($sql);
						$aRow = mysql_fetch_array($query);
						//echo $aRow[numRows]
						
						$totalMerchantRating = 0;
							do {
							$totalMerchantRating = $totalMerchantRating + $merchantComments['rating'];
							} while ($merchantComments = mysql_fetch_assoc($getMerchantComments));
						
							if ($aRow[numRows] != 0) {
							$merchantRating = $totalMerchantRating / $aRow[numRows];
							$merchantRating = round($merchantRating);
							}
						
							if ($navOpt[7] == 1) {
							// html perma
							$merchantLink = $installDir['value'].'stores/'.urltxt($comparison['merchant']).'.html';
							} else {
							// php link
							$merchantLink = $installDir['value'].'index.php?case=stores&merchant='.urltxt($comparison['merchant']);
							}
							if ($aRow[numRows] == 0) {
							$box_content->assign("MERCHANTRATING",'<a href="'.$merchantLink.'">Review this store</a><br />');
							} else {
							$box_content->assign("MERCHANTRATING",'<img src="'.$installDir['value'].'skins/'.$configSkin['value'].'/styleImages/'.$merchantRating.'tic.gif" width="84" height="15" alt="Rating: '.$merchantRating.'" /><br />(<a href="'.$merchantLink.'">'.$aRow[numRows].' reviews</a>)<br />');
							}
						} else {
							$box_content->assign("MERCHANTRATING",'');
						}
					}
					
					
					
					$columnCount++;
					$productCount++;
					if ($columnCount == $totalColumns && $productCount != $pPerPage[4]) {
					$box_content->assign("ENDROW","</tr>\n<tr>");
					$columnCount = 0;
					}
					
					$box_content->parse("prod.td");
		
        } while ($comparison = mysql_fetch_assoc($getComparison));
		
		$box_content->assign("PRICERANGE",' : From '.$thelowprice.$thehighprice);

$box_content->parse("prod");
$box_content = $box_content->text("prod");

} else {
$box_content->parse("noprod");
$box_content = $box_content->text("noprod");
}
?>